<template>
  <div class="user">
    <div class="bg">
      <van-uploader :after-read="changeBg">
        <img class="auto-img" :src="userInfo.userBg" alt="" />
      </van-uploader>
    </div>
    <div class="content">
      <div class="box">
        <div class="show">
          <img class="img auto-img" :src="userInfo.userImg" />
          <div>
            <div class="name">{{ userInfo.nickName }}</div>
            <div class="motto">{{ userInfo.desc }}</div>
          </div>
        </div>
        <ul>
          <li
            class="info"
            v-for="(item, index) in setList"
            :key="index"
            @click="go(item.to)"
          >
            {{ item.title }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "User",

  data() {
    return {
      userInfo: [],
      setList: [
        {
          title: "个人资料",
          to: { name: "UserInfo" },
        },
        {
          title: "我的订单",
          to: { name: "Order" },
        },
        {
          title: "我的收藏",
          to: { name: "Collection" },
        },
        {
          title: "地址管理",
          to: { name: "Address" },
        },
        {
          title: "安全中心",
          to: { name: "Security" },
        },
      ],
    };
  },

  created() {
    // 在session storage中存储tabbar的状态
    this.$util.setSessionItem("tabbarActive", 3);

    // 获取我的用户信息
    this.getMyUserInfo();
  },

  methods: {
    // 获取我的用户信息
    getMyUserInfo() {
      let tokenString = this.$cookies.get("TOKEN");
      this.axios
        .get("findMy", {
          params: {
            tokenString,
          },
        })
        .then((res) => {
          //
          if (res.data.code == 700) {
            this.$toast("请先登录");
            // 清除token
            this.$cookies.remove("TOKEN");
            // 延迟2秒后跳转到登录页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.replace({ name: "Login" });
            }, 2000);
            return;
          }
          if (res.data.code === "A001") {
            this.userInfo = res.data.result[0];
          }
        })
        .catch((err) => {});
    },

    // 跳转到指定页面
    go(to) {
      this.$router.push(to);
    },

    // 修改背景
    changeBg(file) {
      // 此时可以自行将文件上传至服务器
      //
      let tokenString = this.$cookies.get("TOKEN");
      let imgType = file.file.name.split(".")[1];
      //
      let serverBase64Img = file.content.replace(
        /^data:image\/[A-Za-z]+;base64,/,
        ""
      );
      //
      this.axios
        .post("updateUserBg", {
          tokenString,
          imgType,
          serverBase64Img,
        })
        .then((res) => {
          //
          if (res.data.code == 700) {
            this.$toast("请先登录");
            // 清除token
            this.$cookies.remove("TOKEN");
            // 延迟2秒后跳转到登录页面
            let timer = setTimeout(() => {
              clearTimeout(timer);
              this.$router.replace({ name: "Login" });
            }, 2000);
            return;
          }
          if (res.data.code === "I001") {
            this.$toast(res.data.msg);
            // 延迟2秒后更新页面数据
            let timer = setTimeout(() => {
              clearTimeout(timer);
              // 调用查询用户信息方法更新页面数据
              this.getMyUserInfo();
            }, 2000);
          }
        })
        .catch((err) => {});
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.van-uploader {
  width: 100%;
}
/deep/ .van-uploader__input-wrapper {
  width: 100%;
  height: 180px;
}

.user {
  .bg {
    width: 100%;
    height: 180px;
    overflow: hidden;
  }
  .content {
    margin-top: -50px;
    padding: 10px;
    .box {
      position: relative;
      padding: 10px;
      background: rgba(255, 255, 255, 0.8);
      border-top-left-radius: 10px;
      border-top-right-radius: 10px;
      .show {
        display: flex;
        // align-items: center;
        .img {
          margin-right: 20px;
          width: 70px;
          height: 70px;
          border-radius: 50%;
        }
        .name {
          color: #ff594d;
          font-size: 16px;
          font-weight: bold;
          line-height: 30px;
        }
        .motto {
          color: #999;
          font-size: 12px;
          line-height: 35px;
        }
      }
      ul {
        margin-top: 50px;
        li {
          position: relative;
          font-size: 14px;
          line-height: 50px;
          border-bottom: 1px solid #eee;
          &:last-child {
            border-bottom: none;
          }
          &::after {
            position: absolute;
            top: 21px;
            right: 6px;
            display: block;
            width: 6px;
            height: 6px;
            border-top: 1.5px solid #999;
            border-right: 1.5px solid #999;
            transform: rotate(45deg);
            content: "";
          }
        }
      }
    }
  }
}
</style>